﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;

namespace DataAcquisition
{
    internal class SqlHelper
    {
        /// <summary>联接数据库
        /// 
        /// </summary>
        static string DBPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "collect.db");

        //static string connectionString = "Data Source=" + DBPath + ";Version=3;"
        static string DBName = "data_acquisition";
        static string DBAddress = "localhost";
        static string DBPort = "3306";
        static string DBUser = "root";
        static string DBPwd = "root";
        static string connectionString = string.Format("Database={0};Host ={1};Port={2};Username={3};Password={4};SslMode=none;AllowPublicKeyRetrieval=True",
                                                        DBName, DBAddress, DBPort, DBUser, DBPwd);

        /// <summary>错误代码
        /// 
        /// </summary>
        static int ErrorCode = -1;

        public static SqlSugarScope DB = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = connectionString,//连接符字串
            DbType = DbType.MySql,//数据库类型
            IsAutoCloseConnection = true,
            InitKeyType = InitKeyType.Attribute,
        },
            db =>
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响
                };
                db.Aop.OnError = (exp) =>//SQL报错
                {
                    Console.WriteLine("SQL执行错误：【" + exp.Message + "】");
                };
            }
         );

        /// <summary>连接状态
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool State()
        {
            return DB.Ado.IsValidConnection();
        }

        /// <summary>插入数据
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Insertable<T>(List<T> list) where T : class, new()
        {
            try
            {
                int res = DB.Insertable(list).ExecuteCommand();
                return res;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("插入记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>删除记录
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int Deleteable<T>(List<T> list) where T : class, new()
        {
            try
            {
                return DB.Deleteable<T>(list).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("删除记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>更新记录
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Updateable<T>(List<T> list) where T : class, new()
        {
            try
            {
                return DB.Updateable<T>(list).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("更新记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>查询符合条件的记录
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <returns></returns>
        public static List<T> Inquireable<T>(Dictionary<string, Object>? where)
        {
            try
            {
                ISugarQueryable<T>? sugarQueryable = null;
                if (where != null)
                {
                    sugarQueryable = DB.Queryable<T>().WhereColumns(where);
                }
                else
                {
                    sugarQueryable = DB.Queryable<T>();
                }
                List<T> list = sugarQueryable.ToList();
                return list;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("查询记录异常：" + ex.Message);
                return new List<T>();
            }
        }
    }
}
